# Maintainer: Bruno Pagani <archange@archlinux.org>

pkgname=cozy-stack
pkgver=1.5.8
pkgrel=1
epoch=1
pkgdesc="Digital home: brings all your web services in the same private space – Stack component"
arch=(loong64 x86_64)
url="https://cozy.io"
license=(AGPL3)
backup=(etc/cozy/cozy.yml)
depends=(couchdb imagemagick git)
makedepends=(go)
#optdepends=('cozy-coclyco: X.509 certificates management for instances'
optdepends=('nodejs: required for konnectors'
            'nsjail: run konnectors isolated'
            'smtp-forwarder: to allow sending mail to users')
source=(https://github.com/cozy/cozy-stack/archive/${pkgver}/${pkgname}-${pkgver}.tar.gz
        cozy.yml
        ${pkgname}.service
        ${pkgname}.sysusers
        ${pkgname}.tmpfiles)
sha256sums=('c9865db8a46dcbfd4214a5ce5d14590cb7b56410edd01a5ae967528121c2bc4d'
            'a6ae871ec726f81d091918dffae4025b993656551185662242dcc2f7de4516c3'
            '4ef3e901725167edd970a7f288e60f3729c12ae8a07289b6bb09bc96d6851b42'
            'a6bea52350e85163c3141509a52903223fa0f6e7390b1b1f9336c326a8fff984'
            '04043ed0b2bf1c811417eec3b89a049f5353ad16f032497ff5c9a610eafa879d')

prepare() {
    cd ${pkgname}-${pkgver}
    go mod vendor
}

build() {
    cd ${pkgname}-${pkgver}
    export CGO_CPPFLAGS="${CPPFLAGS}"
    export CGO_CFLAGS="${CFLAGS}"
    export CGO_CXXFLAGS="${CXXFLAGS}"
    export CGO_LDFLAGS="${LDFLAGS}"
    export GOFLAGS="-buildmode=pie -trimpath -mod=readonly -modcacherw"
    go build -v \
        -ldflags "-linkmode external
                  -X github.com/cozy/cozy-stack/pkg/config.Version=${pkgver} \
                  -X github.com/cozy/cozy-stack/pkg/config.BuildTime=$(date --utc --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ") \
                  -X github.com/cozy/cozy-stack/pkg/config.BuildMode=production" \
        -o ${pkgname}
}

package() {
    install -Dm644 cozy.yml -t "${pkgdir}"/etc/cozy/
    install -Dm644 ${pkgname}.service -t "${pkgdir}"/usr/lib/systemd/system/
    install -Dm644 ${pkgname}.sysusers "${pkgdir}"/usr/lib/sysusers.d/${pkgname}.conf
    install -Dm644 ${pkgname}.tmpfiles "${pkgdir}"/usr/lib/tmpfiles.d/${pkgname}.conf
    cd ${pkgname}-${pkgver}
    install -Dm755 ${pkgname} -t "${pkgdir}"/usr/bin/
    install -Dm644 cozy.example.yaml -t "${pkgdir}"/usr/share/cozy/
    install -Dm755 scripts/konnector-node-run.sh -t "${pkgdir}"/usr/share/cozy/
    install -Dm755 scripts/konnector-nsjail-node12-run.sh "${pkgdir}"/usr/share/cozy/konnector-nsjail-run.sh
}
